package love.yuanmengrobot.qianyugarden.interceptor;
/*
 *   @Studio:CHJY · 源梦科技
 *   @Author:CHJY创幻
 *   @Ide:IntelliJ IDEA
 *   @Creation time:2021/7/31 21:15
 */

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginHandlerInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Object user = request.getSession().getAttribute("UserInfo");
        System.out.println(request.getRequestURI());
        if (user == null
                && !request.getRequestURI().contains("/bus/toLogin")
                &&!request.getRequestURI().contains("/bus/toindex")
                &&!request.getRequestURI().contains("/toMainContent")
                &&!request.getRequestURI().contains("/bus/toSignup")
                &&!request.getRequestURI().contains("/loginLog/signup")
                &&!request.getRequestURI().contains("/yuanwen/getViewBrowseAritcle")
                &&!request.getRequestURI().contains("/bus/toBookList")
                &&!request.getRequestURI().contains("/bus/toFindResult")
                &&!request.getRequestURI().contains("/download/bookList")
//                &&!request.getRequestURI().contains("/download/bookDetail")
                &&!request.getRequestURI().contains("/loginLog/login")
                &&!request.getRequestURI().contains("/error")
                ) {
            System.out.println("无权限请先登录");
            request.setAttribute("msg", "无权限请先登录");
            request.getRequestDispatcher("/bus/toLogin").forward(request, response);
            return false;
        } else {
            return true;
        }
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
